jQuery(document).ready(function(){
	jQuery("#cmotool-checkouts-panel").delegate('a.cmotool-checkin', 'click', function(){
		var ticketId = jQuery(this).parents('#cmotool-checkouts-panel').data("ticketId");
		var filename = jQuery(this).data("filename");
		console.log('User chooses: ' + ticketId);
		console.log('... ' + filename); 
		self.port.emit("file.checkin", {"ticket":ticketId,"files":[filename]});
	});
	
	jQuery("#cmotool-checkouts-panel").delegate('a.cmotool-checkout', 'click', function(){
		var ticketId = jQuery(this).parents('#cmotool-checkouts-panel').data("ticketId");
		var filename = jQuery(jQuery(this).attr("rel")).val();
		console.log('User chooses: ' + ticketId);
		console.log('... ' + filename);		
		self.port.emit("file.checkout", {"ticket":ticketId,"files":[filename]});
	});
	
	jQuery("#cmotool-checkouts-panel span.cmotool-sync").click(function(){
		var ticketId = jQuery(this).parents('#cmotool-checkouts-panel').data("ticketId");
		self.port.emit("ticket.sync", {"ticket":ticketId,"url":document.location.href});						
	});
	
	jQuery("#cmotool-checkouts-panel span.cmotool-svn-ci").click(function(){
		console.log('Generating svn ci command');
		
		var tokens = ["svn ci"];
		jQuery("#cmotool-checkouts-panel .cmotool-checkouts-list li span").each(function(index) {
			tokens.push($(this).text());
		});
		if (tokens.length>1) {
			jQuery("#cmotool-checkouts-panel div.cmotool-message").html(tokens.join(' '));
			console.log("Copied to clipboard");
			self.port.emit('clipboard.copy',tokens.join(" "));
		} else {
			jQuery("#cmotool-checkouts-panel div.cmotool-message").html('No files to commit');
		}
	});
	
	jQuery("#cmotool-checkouts-panel span.cmotool-trig").click(function(){
		console.log('Generating trig command');
		
		var tokens = ["trig"];
		jQuery("#cmotool-checkouts-panel .cmotool-checkouts-list li span").each(function(index) {
			tokens.push($(this).text());
		});
		if (tokens.length>1) {
			tokens.push('--final');
		
			jQuery("#cmotool-checkouts-panel div.cmotool-message").html(tokens.join(' '));
			console.log("Copied to clipboard");
			self.port.emit('clipboard.copy',tokens.join(" "));
		} else {
			jQuery("#cmotool-checkouts-panel div.cmotool-message").html('No files to trig');
		}
	});
	// WORK COMPLETED
	self.port.on("file.checkin.complete", function(checkoutData){
		console.log(checkoutData);
		if (checkoutData == null) {
			busy(false);
			return;
		}
		updateTicketCheckoutTable(checkoutData.files);
		busy(false);
	});
	
	self.port.on("file.checkout.complete", function(checkoutData){
		console.log(checkoutData);
		if (checkoutData == null) {
			busy(false);
			return;
		}
		updateTicketCheckoutTable(checkoutData.files);
		jQuery('#cmotool-new-checkout-filename').val('');
		busy(false);
	});
	
	self.port.on("ticket.sync.complete", function(checkoutData){
		if (checkoutData == null) {
			busy(false);
			return;
		}
		console.log(checkoutData);
		busy(false);
	});
	// WORK STARTED
	self.port.on("file.checkin.started", function(){
		busy(true);
	});
	
	self.port.on("file.checkout.started", function(){
		busy(true);
	});
	
	self.port.on("ticket.sync.started", function(){
		busy(true);
	});
});

function updateTicketCheckoutTable(files) {
	console.log('Updating checkout table...');
	
	var list = jQuery('#cmotool-checkouts-panel ol.cmotool-checkouts-list').empty();
	var table = '';
	for(var i=0;i<files.length;i++)
		table += "	<li><span>" + normalizeFilename(files[i]) + "</span><a class='cmotool-checkin' data-filename=" + files[i] + ">Check in</a></li>";
	list.html(table);
	
	console.log('Updating checkout table completed');
}

function normalizeFilename(filename) {
	return filename.replace('/home/webuser/site/','');
}

function busy(isBusy){
	if (isBusy)
		jQuery('#cmotool-checkouts-panel .cmotool-loading').show();
	else
		jQuery('#cmotool-checkouts-panel .cmotool-loading').hide();
}